﻿<UserControl x:Class="LINQPad.OpenAccess.UI.View.ConnectionSettingsHostView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:conv="clr-namespace:LINQPad.OpenAccess.UI.Converters"
             xmlns:local="clr-namespace:LINQPad.OpenAccess.UI.View"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ControlTemplate x:Key="RequiredProviderPresenter" TargetType="Control">
            <Grid Margin="5 2">
                <TextBox HorizontalAlignment="Stretch" Style="{DynamicResource ValidatableReadOnlyTextBoxStyle}"
                         Text="{Binding ProviderName, Mode=OneWay, ValidatesOnDataErrors=True}" />
                <TextBlock Margin="5 0" x:Name="NoProviderSetErrorHint" Text="no provider or backend selected !"
                           VerticalAlignment="Center" Visibility="Collapsed"
                           Style="{DynamicResource ErrorHintTextBlockStyle}" IsHitTestVisible="False"
                            />
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding ProviderName, FallbackValue={x:Null}}" Value="{x:Null}">
                    <Setter TargetName="NoProviderSetErrorHint" Property="Visibility" Value="Visible" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <ControlTemplate x:Key="ConnectionConfiguratorSelector" TargetType="Control">
            <Grid Margin="5 2">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <ListBox MinWidth="150" VerticalAlignment="Top" MinHeight="25" 
                         Style="{DynamicResource RadioButtonListBoxStyle}"
                         ItemsSource="{Binding AvailableConnectionSettings}" 
                         SelectedItem="{Binding ConnectionSettings, Mode=TwoWay}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" ToolTip="{Binding Description}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <TextBlock Margin="5 2" x:Name="SelectorErrorHint" Text="no configurator(s) available !"
                           VerticalAlignment="Center" Visibility="Collapsed"
                           Style="{DynamicResource ErrorHintTextBlockStyle}" IsHitTestVisible="False"
                            />
                <Button Grid.Row="1" DataContext="{Binding ConnectionSettings}" Style="{DynamicResource TestConnectionButtonStyle}" />
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding HasAvailableConnectionSettings, FallbackValue=False}" Value="False">
                    <Setter TargetName="SelectorErrorHint" Property="Visibility" Value="Visible" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>
    <DockPanel>
        <GroupBox DockPanel.Dock="Top" Header="required provider">
            <Control Template="{StaticResource RequiredProviderPresenter}" />
        </GroupBox>
        <GroupBox DockPanel.Dock="Top" Header="connection">
            <Control Template="{StaticResource ConnectionConfiguratorSelector}" />
        </GroupBox>
        <GroupBox DockPanel.Dock="Top" Header="settings">
            <local:ConnectionSettingsViewHost ConnectionSettings="{Binding ConnectionSettings, ValidatesOnDataErrors=True}" />
        </GroupBox>
    </DockPanel>
</UserControl>
